package com.sunkyc.module.oss.config;

import cn.hutool.extra.spring.SpringUtil;
import io.github.artislong.core.StandardOssClient;
import io.github.artislong.core.minio.MinioOssClient;
import io.github.artislong.core.minio.MinioOssConfiguration;
import io.github.artislong.core.minio.MinioOssProperties;
import io.github.artislong.core.minio.model.MinioOssClientConfig;
import io.github.artislong.core.minio.model.MinioOssConfig;
import io.minio.MinioClient;
import lombok.Data;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;


@Configuration
@Data
public class MiniOssConfig {




    public static final String DEFAULT_BEAN_NAME = "sunkycMinIoClient";
    @Bean(name = DEFAULT_BEAN_NAME)
    public MinioOssClient minioOssClient(MinioOssConfig minioOssConfig) {
        MinioOssClient minioOssClient = new MinioOssClient(minioClient(minioOssConfig), minioOssConfig);
        return minioOssClient;
    }
    public MinioClient minioClient(MinioOssConfig minioOssConfig) {
        MinioOssClientConfig clientConfig = minioOssConfig.getClientConfig();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(clientConfig.getConnectTimeout(), TimeUnit.MILLISECONDS)
                .writeTimeout(clientConfig.getWriteTimeout(), TimeUnit.MILLISECONDS)
                .readTimeout(clientConfig.getReadTimeout(), TimeUnit.MILLISECONDS)
                .callTimeout(clientConfig.getCallTimeout(), TimeUnit.MILLISECONDS)
                .followRedirects(clientConfig.isFollowRedirects())
                .followSslRedirects(clientConfig.isFollowSslRedirects())
                .retryOnConnectionFailure(clientConfig.isRetryOnConnectionFailure())
                .pingInterval(clientConfig.getPingInterval(), TimeUnit.MILLISECONDS)
                .build();
        return MinioClient.builder()
                .endpoint(minioOssConfig.getEndpoint())
                .credentials(minioOssConfig.getAccessKey(), minioOssConfig.getSecretKey())
                .httpClient(okHttpClient)
                .build();
    }





}
